<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="学生电话" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入学生电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="家长" prop="parents">
        <el-input v-model="queryParams.parents" placeholder="请输入家长" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="家长电话" prop="parentsTel">
        <el-input v-model="queryParams.parentsTel" placeholder="请输入家长电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="住址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入住址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="老师" prop="teacherName">
        <el-input v-model="queryParams.teacherName" placeholder="请输入老师" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="销售" prop="salesName">
        <el-input v-model="queryParams.salesName" placeholder="请输入销售" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="意向" prop="intention">
        <el-select v-model="queryParams.intention" placeholder="请选择类型">
          <el-option v-for="dict in dict.type.yx_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['jiaopei:student:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['jiaopei:student:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['jiaopei:student:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['jiaopei:student:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="true" min-width="100" show-overflow-tooltip />
      <el-table-column label="姓名" align="center" prop="name" min-width="100" show-overflow-tooltip />
      <el-table-column label="学生电话" align="center" prop="tel" min-width="110" show-overflow-tooltip />
      <el-table-column label="生日" align="center" prop="birthday" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="家长" align="center" prop="parents" min-width="100" show-overflow-tooltip />
      <el-table-column label="家长电话" align="center" prop="parentsTel" min-width="110" show-overflow-tooltip />
      <el-table-column label="住址" align="center" prop="address" min-width="160" show-overflow-tooltip />
      <el-table-column label="老师" align="center" prop="teacherVo.nickName" min-width="80" show-overflow-tooltip />
      <el-table-column label="销售" align="center" prop="salesVo.nickName" min-width="80" show-overflow-tooltip />
      <el-table-column label="意向" align="center" prop="intention">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yx_type" :value="scope.row.intention" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" min-width="100" show-overflow-tooltip />
      <el-table-column label="家长账号" align="center" prop="parentsName" min-width="100" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createdDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" min-width="240" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            @click="showKc(scope.row)"
            v-hasPermi="['jiaopei:curriculumStudent:list']"
          >课程</el-button> -->
          <el-button size="mini" type="text" @click="showDialog1(scope.row)"
            v-hasPermi="['jiaopei:curriculumStudent:list']">课程</el-button>
          <el-button
                size="mini"
                type="text"
                @click="handleBindParents(scope.row)"
              >绑定家长</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['jiaopei:student:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['jiaopei:student:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改学员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="家长" prop="parents">
          <el-input v-model="form.parents" placeholder="请输入家长" />
        </el-form-item>
        <el-form-item label="家长电话" prop="parentsTel">
          <el-input v-model="form.parentsTel" placeholder="请输入家长电话" />
        </el-form-item>
        <el-form-item label="学生电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入学生电话" />
        </el-form-item>

        <el-form-item label="老师" prop="teacherId">
          <group-select class="form-item-input-style" v-model="form.teacherId" :group_list="['user']"
            :group_query="teacherGroupQuery" placeholder="请选择老师" />
        </el-form-item>
        <el-form-item label="销售" prop="salesId">
          <group-select class="form-item-input-style" v-model="form.salesId" :group_query="salesGroupQuery"
            :group_list="['user']" placeholder="请选择销售" />
        </el-form-item>





        <el-form-item label="生日" prop="birthday">
          <el-date-picker clearable v-model="form.birthday" type="date" value-format="yyyy-MM-dd" placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入住址" />
        </el-form-item>


        <el-form-item label="意向" prop="intention">
          <el-select v-model="form.intention" placeholder="请选择类型">
            <el-option v-for="dict in dict.type.yx_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <!--        <el-form-item label="创建时间" prop="createdDate">-->
        <!--          <el-date-picker clearable-->
        <!--            v-model="form.createdDate"-->
        <!--            type="datetime"-->
        <!--            value-format="yyyy-MM-dd HH:mm:ss"-->
        <!--            placeholder="请选择创建时间">-->
        <!--          </el-date-picker>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <select-user ref="select" @ok="selectUserOk" />
    <select-user ref="select1" @ok="selectUserOk1" />

    <el-dialog title="课程列表" width="90%" v-if="dialogShow1" :visible.sync="dialogShow1">
      <curriculumStudentList :studentId="studentId"></curriculumStudentList>
    </el-dialog>
    <BindParents ref="bindParents"  @refresh="getList" ></BindParents>
  </div>
</template>

<script>
import { listStudent, listSalesStudent, getStudent, delStudent, addStudent, updateStudent } from "@/api/jiaopei/student";

import selectUser from "@/views/jiaopei/class/selectUser";
import curriculumStudentList from "@/views/jiaopei/curriculumStudent/index";
import BindParents from "@/views/jiaopei/student/bindParents";
import groupSelect from "@/views/components/groupSelect";
export default {
  name: "Student",
  dicts: ['yx_type'],
  components: { selectUser, curriculumStudentList, groupSelect, BindParents },
  computed: {
    teacherGroupQuery() {
      var value = {
        'user': {
          teacher: '0,1'
        }
      }
      return value
    },
    salesGroupQuery() {
      var value = {
        'user': {
          teacher: '0,1,2'
        }
      }
      return value
    }
  },
  data() {
    return {
      studentId: 0,
      dialogShow1: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        tel: undefined,
        parents: undefined,
        parentsTel: undefined,
        address: undefined,
        salesName: undefined,
        teacherName: undefined,
        intention: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        // teacherName: [
        //   { required: true, message: "老师不能为空", trigger: "blur" }
        // ],
        // birthday: [
        //   { required: true, message: "生日不能为空", trigger: "blur" }
        // ],
        parents: [
          { required: true, message: "家长不能为空", trigger: "blur" }
        ],
        parentsTel: [
          { required: true, message: "家长电话不能为空", trigger: "blur" }
        ],
        // address: [
        //   { required: true, message: "住址不能为空", trigger: "blur" }
        // ],
        createdDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学员列表 */
    getList() {
      this.loading = true;

      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });


    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        tel: undefined,
        birthday: undefined,
        parents: undefined,
        parentsTel: undefined,
        address: undefined,
        remarks: undefined,
        intention: undefined,
        salesId: undefined,
        salesName: undefined,
        teacherId: undefined,
        teacherName: undefined,
        createdDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.form.salesId = `${this.form.salesId}`
        this.form.teacherId = `${this.form.teacherId}`
        this.open = true;
        this.title = "修改学员";

        if (this.form['teacherVo']) {
          this.form.teacherName = this.form['teacherVo']['nickName'];
        }
        if (this.form['salesVo']) {
          this.form.salesName = this.form['salesVo']['nickName'];
        }

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delStudent(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jiaopei/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },
    showKc(row) {
      const studentId = row.id;
      const studentName = row['name'];
      this.$router.push({ path: '/yewu/curriculumStudent', query: { 'studentId': studentId, 'studentName': studentName } })
    },
    showDialog1(row) {
      this.studentId = row.id;
      this.dialogShow1 = true;
    },
    handleBindParents(row){
      this.studentId = row.id;
      this.$refs.bindParents.show(null,false,{studentId:row.id});
    },
    openSelectUser() {
      this.$refs.select.show();
    },
    selectUserOk(row) {
      this.form.teacherId = row['userId'];
      this.form.teacherName = row['nickName'];
      this.$forceUpdate();
    },
    openSelectUser1() {
      this.$refs.select1.show(true, '2');
    },
    selectUserOk1(row) {
      this.form.salesId = row['userId'];
      this.form.salesName = row['nickName'];
      this.$forceUpdate();
    }
  }
};
</script>
